{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import KFold\n",
    "from sklearn.metrics import auc, roc_curve\n",
    "from lightgbm import LGBMRegressor\n",
    "from catboost import CatBoostRegressor\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "import datetime\n",
    "np.random.seed(2020) \n",
    "\n",
    "train_file_name = 'G://classStudy/II 2/机器学习/大作业/贷款违约预测/train.csv'\n",
    "test_file_name = 'G://classStudy/II 2/机器学习/大作业/贷款违约预测/testA.csv'\n",
    "\n",
    "df_train = pd.read_csv(train_file_name)\n",
    "df_test = pd.read_csv(test_file_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import LabelEncoder\n",
    "labelEncoder = LabelEncoder()\n",
    "df_train['grade'] = labelEncoder.fit_transform(df_train['grade'])\n",
    "df_train['grade'] = labelEncoder.fit_transform(df_train['grade'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         4\n",
       "1         3\n",
       "2         3\n",
       "3         0\n",
       "4         2\n",
       "         ..\n",
       "799995    2\n",
       "799996    0\n",
       "799997    2\n",
       "799998    0\n",
       "799999    1\n",
       "Name: grade, Length: 800000, dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train['grade']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0           2 years\n",
       "1           5 years\n",
       "2           8 years\n",
       "3         10+ years\n",
       "4               NaN\n",
       "            ...    \n",
       "799995      7 years\n",
       "799996    10+ years\n",
       "799997    10+ years\n",
       "799998    10+ years\n",
       "799999      5 years\n",
       "Name: employmentLength, Length: 800000, dtype: object"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train['employmentLength']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "employmentLength = ['< 1 year','1 year','2 years',\n",
    "                    '3 years',  '4 years', '5 years', \n",
    "                    '6 years', '8 years', '7 years','9 years','10+ years']\n",
    "j = 0\n",
    "for i in employmentLength:\n",
    "    df_train['employmentLength'] = df_train['employmentLength'].replace(i, j)\n",
    "    j += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = pd.crosstab(df_train.subGrade, df_train.isDefault)\n",
    "_[\"yp\"] = _[1]/(_[0]+_[1])\n",
    "_.reset_index(inplace=True)\n",
    "_.sort_values(by=\"yp\", inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>isDefault</th>\n",
       "      <th>subGrade</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>yp</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A1</td>\n",
       "      <td>25082</td>\n",
       "      <td>827</td>\n",
       "      <td>0.031919</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A2</td>\n",
       "      <td>21113</td>\n",
       "      <td>1011</td>\n",
       "      <td>0.045697</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A3</td>\n",
       "      <td>21389</td>\n",
       "      <td>1266</td>\n",
       "      <td>0.055882</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A4</td>\n",
       "      <td>28849</td>\n",
       "      <td>2079</td>\n",
       "      <td>0.067221</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A5</td>\n",
       "      <td>34796</td>\n",
       "      <td>3249</td>\n",
       "      <td>0.085399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>B1</td>\n",
       "      <td>38020</td>\n",
       "      <td>4362</td>\n",
       "      <td>0.102921</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>B2</td>\n",
       "      <td>39262</td>\n",
       "      <td>4965</td>\n",
       "      <td>0.112262</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>B3</td>\n",
       "      <td>42319</td>\n",
       "      <td>6281</td>\n",
       "      <td>0.129239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>B4</td>\n",
       "      <td>42156</td>\n",
       "      <td>7360</td>\n",
       "      <td>0.148639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>B5</td>\n",
       "      <td>40854</td>\n",
       "      <td>8111</td>\n",
       "      <td>0.165649</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>C1</td>\n",
       "      <td>41049</td>\n",
       "      <td>9714</td>\n",
       "      <td>0.191360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>C2</td>\n",
       "      <td>37330</td>\n",
       "      <td>9738</td>\n",
       "      <td>0.206892</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>C3</td>\n",
       "      <td>34701</td>\n",
       "      <td>10050</td>\n",
       "      <td>0.224576</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>C4</td>\n",
       "      <td>33199</td>\n",
       "      <td>11073</td>\n",
       "      <td>0.250113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>C5</td>\n",
       "      <td>29733</td>\n",
       "      <td>10531</td>\n",
       "      <td>0.261549</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>D1</td>\n",
       "      <td>22049</td>\n",
       "      <td>8489</td>\n",
       "      <td>0.277982</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>D2</td>\n",
       "      <td>18634</td>\n",
       "      <td>7894</td>\n",
       "      <td>0.297572</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>D3</td>\n",
       "      <td>16293</td>\n",
       "      <td>7117</td>\n",
       "      <td>0.304015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>D4</td>\n",
       "      <td>14314</td>\n",
       "      <td>6825</td>\n",
       "      <td>0.322863</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>D5</td>\n",
       "      <td>11867</td>\n",
       "      <td>5971</td>\n",
       "      <td>0.334735</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>E1</td>\n",
       "      <td>9068</td>\n",
       "      <td>4996</td>\n",
       "      <td>0.355233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>E2</td>\n",
       "      <td>7942</td>\n",
       "      <td>4804</td>\n",
       "      <td>0.376903</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>E3</td>\n",
       "      <td>6692</td>\n",
       "      <td>4233</td>\n",
       "      <td>0.387460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>E4</td>\n",
       "      <td>5543</td>\n",
       "      <td>3730</td>\n",
       "      <td>0.402243</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>E5</td>\n",
       "      <td>5026</td>\n",
       "      <td>3627</td>\n",
       "      <td>0.419161</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>F1</td>\n",
       "      <td>3398</td>\n",
       "      <td>2527</td>\n",
       "      <td>0.426498</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>F2</td>\n",
       "      <td>2361</td>\n",
       "      <td>1979</td>\n",
       "      <td>0.455991</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>F3</td>\n",
       "      <td>1943</td>\n",
       "      <td>1634</td>\n",
       "      <td>0.456807</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>G1</td>\n",
       "      <td>939</td>\n",
       "      <td>820</td>\n",
       "      <td>0.466174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>F4</td>\n",
       "      <td>1494</td>\n",
       "      <td>1365</td>\n",
       "      <td>0.477440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>G2</td>\n",
       "      <td>639</td>\n",
       "      <td>592</td>\n",
       "      <td>0.480910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>F5</td>\n",
       "      <td>1216</td>\n",
       "      <td>1136</td>\n",
       "      <td>0.482993</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>G3</td>\n",
       "      <td>470</td>\n",
       "      <td>508</td>\n",
       "      <td>0.519427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>G4</td>\n",
       "      <td>359</td>\n",
       "      <td>392</td>\n",
       "      <td>0.521971</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>G5</td>\n",
       "      <td>291</td>\n",
       "      <td>354</td>\n",
       "      <td>0.548837</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "isDefault subGrade      0      1        yp\n",
       "0               A1  25082    827  0.031919\n",
       "1               A2  21113   1011  0.045697\n",
       "2               A3  21389   1266  0.055882\n",
       "3               A4  28849   2079  0.067221\n",
       "4               A5  34796   3249  0.085399\n",
       "5               B1  38020   4362  0.102921\n",
       "6               B2  39262   4965  0.112262\n",
       "7               B3  42319   6281  0.129239\n",
       "8               B4  42156   7360  0.148639\n",
       "9               B5  40854   8111  0.165649\n",
       "10              C1  41049   9714  0.191360\n",
       "11              C2  37330   9738  0.206892\n",
       "12              C3  34701  10050  0.224576\n",
       "13              C4  33199  11073  0.250113\n",
       "14              C5  29733  10531  0.261549\n",
       "15              D1  22049   8489  0.277982\n",
       "16              D2  18634   7894  0.297572\n",
       "17              D3  16293   7117  0.304015\n",
       "18              D4  14314   6825  0.322863\n",
       "19              D5  11867   5971  0.334735\n",
       "20              E1   9068   4996  0.355233\n",
       "21              E2   7942   4804  0.376903\n",
       "22              E3   6692   4233  0.387460\n",
       "23              E4   5543   3730  0.402243\n",
       "24              E5   5026   3627  0.419161\n",
       "25              F1   3398   2527  0.426498\n",
       "26              F2   2361   1979  0.455991\n",
       "27              F3   1943   1634  0.456807\n",
       "30              G1    939    820  0.466174\n",
       "28              F4   1494   1365  0.477440\n",
       "31              G2    639    592  0.480910\n",
       "29              F5   1216   1136  0.482993\n",
       "32              G3    470    508  0.519427\n",
       "33              G4    359    392  0.521971\n",
       "34              G5    291    354  0.548837"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_train = pd.merge(df_train, _[[\"subGrade\", \"yp\"]], on=\"subGrade\", how=\"left\")\n",
    "df_train['subGrade'] = labelEncoder.fit_transform(df_train['subGrade'])\n",
    "df_train['issueDate'] = pd.to_datetime(df_train['issueDate'],format='%Y-%m-%d')\n",
    "startdate = datetime.datetime.strptime('2007-06-01', '%Y-%m-%d')\n",
    "df_train['issueDateDT'] = df_train['issueDate'].apply(lambda x: x-startdate).dt.days\n",
    "df_train['earliesCreditLine'] = df_train['earliesCreditLine'].apply(lambda s: int(s[-4:]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         2001\n",
       "1         2002\n",
       "2         2006\n",
       "3         1999\n",
       "4         1977\n",
       "          ... \n",
       "799995    2011\n",
       "799996    1989\n",
       "799997    2002\n",
       "799998    1994\n",
       "799999    2002\n",
       "Name: earliesCreditLine, Length: 800000, dtype: int64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train['earliesCreditLine']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "tags = ['loanAmnt', 'term', 'interestRate', 'installment', 'grade',\n",
    "       'subGrade', 'employmentTitle', 'employmentLength', 'homeOwnership',\n",
    "       'annualIncome', 'verificationStatus', 'issueDateDT', 'earliesCreditLine',\n",
    "       'purpose', 'postCode', 'regionCode', 'dti', 'delinquency_2years',\n",
    "       'ficoRangeLow', 'ficoRangeHigh', 'openAcc', 'pubRec',\n",
    "       'pubRecBankruptcies', 'revolBal', 'revolUtil', 'totalAcc',\n",
    "       'initialListStatus', 'applicationType', 'title',\n",
    "       'n0', 'n1', 'n2', 'n4', 'n5', 'n6', 'n7', 'n8',\n",
    "       'n9', 'n10', 'n11', 'n12', 'n13', 'n14','yp']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_test['grade'] = labelEncoder.fit_transform(df_test['grade'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "employmentLength = ['< 1 year','1 year','2 years',\n",
    "                    '3 years',  '5 years', '4 years', \n",
    "                    '6 years', '8 years', '7 years','9 years','10+ years']\n",
    "j = 0\n",
    "for i in employmentLength:\n",
    "    df_test['employmentLength'] = df_test['employmentLength'].replace(i, j)\n",
    "    j += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_test = pd.merge(df_test, _[[\"subGrade\", \"yp\"]], on=\"subGrade\", how=\"left\")\n",
    "df_test['subGrade'] = labelEncoder.fit_transform(df_test['subGrade'])\n",
    "df_test['issueDate'] = pd.to_datetime(df_test['issueDate'],format='%Y-%m-%d')\n",
    "startdate = datetime.datetime.strptime('2007-06-01', '%Y-%m-%d')\n",
    "df_test['issueDateDT'] = df_test['issueDate'].apply(lambda x: x-startdate).dt.days\n",
    "df_test['earliesCreditLine'] = df_test['earliesCreditLine'].apply(lambda s: int(s[-4:]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "Standard_scaler = StandardScaler()\n",
    "Standard_scaler.fit(df_train[tags].values)\n",
    "x = Standard_scaler.transform(df_train[tags].values)\n",
    "x_ = Standard_scaler.transform(df_test[tags].values)\n",
    "y = df_train['isDefault'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "lgbr = LGBMRegressor(num_leaves=30\n",
    "                        ,max_depth=10\n",
    "                        ,learning_rate=0.01\n",
    "                        ,n_estimators=13000\n",
    "                        ,subsample_for_bin=5000\n",
    "                        ,min_child_samples=200\n",
    "                        ,colsample_bytree=.2\n",
    "                        ,reg_alpha=.1\n",
    "                        ,reg_lambda=.1\n",
    "                        ,seed=2020                       \n",
    "                        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "cat = CatBoostRegressor(depth=9, \n",
    "                            l2_leaf_reg=1, \n",
    "                            learning_rate=0.01, \n",
    "                            eval_metric = 'AUC' ,\n",
    "                            border_count = 128, \n",
    "                            bagging_temperature = 0.9 , \n",
    "                            n_estimators=16000,\n",
    "                            early_stopping_rounds=500, \n",
    "                            subsample = 0.9,\n",
    "                            random_seed=1,\n",
    "                            verbose = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import VotingRegressor\n",
    "\n",
    "rg_model = VotingRegressor([('lgb', lgbr), ('catboost', cat)],n_jobs=12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "rg_model.fit(x,y)\n",
    "pre = pd.DataFrame(rg_model.predict(x_),columns=['isDefault'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = pd.concat([df_test['id'],pre],axis = 1)\n",
    "results.to_csv('submit.csv', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
